<template>
	<view class="u-page">
		<view class="formbox">	
			<view class="item">
				<view class="title">选择客户：</view>
				<view class="value picker" @click="$navigateTo" data-url="admin/store?checkCustomer=1">
					<text v-if="customer" class="tag-info">{{customer | parseName}}</text>
					<text v-else>未选择</text>
					<u-icon name="arrow-right"></u-icon>
				</view>
			</view>
			<view class="item">
				<view class="title">选择卡券：</view>
				<view class="value picker">
					<picker mode="selector" :range="coupon" range-key="title" @change="changeCoupon">
						<view class="uni-input">{{coupon[coupon_index].title}}</view>
					</picker>
					<u-icon name="arrow-right"></u-icon>
				</view>
			</view>
			<view class="item">
				<view class="title">发放数量：</view>
				<view class="value">
					<u-input placeholder="请输入发放数量" v-model="number" type="number">
						<template slot="suffix">张</template>
					</u-input>
				</view>
			</view>
			<view class="submit">
				<u-button type="primary" text="提交" @click="submit"></u-button>
			</view>
		</view>
	</view>
</template>

<script>
	import {baseUrl,methods} from '@/utils/methods.js';
	import {rand} from '@/utils/util.js';
	export default {
		data() {
			return {
				customer:'',
				coupon:[],
				coupon_index:0,
				number:1
			}
		},
		filters:{
			parseName:function(customer){
				if(customer.customer_type=='family'){
					return customer.address;
				}
				if(customer.customer_type=='company'){
					return customer.name;
				}
			}
		},
		onLoad() {
			this.getCoupon();
		},
		onShow() {
			let customer=uni.getStorageSync('setCustomer') || '';
			if(customer){
				this.customer=customer;
				uni.setStorageSync('setCustomer','');
			}
		},
		methods: {
			...methods,
			changeCoupon:function(e){
				this.coupon_index=parseInt(e.detail.value);
				if(this.coupon[this.coupon_index].type=='card'){
					this.balance=parseInt(this.coupon[this.coupon_index].balance);
				}
			},
			getCoupon:function(){
				this.$get('admin/admin/coupon').then(res=>{
					this.coupon=res;
					let obj={
						detail:{
							value:0
						}
					}
					this.changeCoupon(obj);
				});
			},
			submit:function(){
				if(!this.customer){
					this.$msg('请选择客户');
					return;
				}
				this.$post('admin/admin/sendCoupon',{
					customer_id:this.customer.id,
					coupon_id:this.coupon[this.coupon_index].id,
					number:this.number
				},true).then(res=>{
					this.$navigateBack(1500,true);
				});
			}
		}
	} 
</script>

<style lang="scss">
	@import "../common/form.scss";
	.u-page{
		padding-bottom: 100rpx;
	}
</style>